import request from 'request'

export const GET = (url) =>{
    return new Promise((resolve, reject) => 
        request.get(url, (err, response, body) => {
            if (err) {
                reject(err);
            } else if(typeof body=='string'){
                resolve(JSON.parse(body), response);
            }else {
                resolve(body, response);
            }
        })
    );
}

export const POST = (url, data) =>{
    return new Promise((resolve, reject) => 
        request.post(url, {
            json: true,
            headers: {
                "content-type": "application/json; charset=UTF-8",
            },
            body : data
        }, 
        (err, response, body) => {
            if (err) {
                reject(err);
            } else if(typeof body=='string'){
				try{
					resolve(JSON.parse(body), response);
				}catch(error){
					resolve(body, response);
				}
            }else {
                resolve(body, response);
            }
        })
    );
}
